package com.test.videoplayer.activity.play;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dueeeke.videocontroller.StandardVideoController;
import com.dueeeke.videoplayer.listener.OnVideoViewStateChangeListener;
import com.dueeeke.videoplayer.player.VideoView;
import com.dueeeke.videoplayer.player.VideoViewManager;
import com.dueeeke.videoplayer.util.L;
import com.test.videoplayer.R;
import com.test.videoplayer.helper.NetSpeedTimer;

import java.net.URLEncoder;

import static com.dueeeke.videoplayer.player.VideoView.PLAYER_NORMAL;

/**
 * 多开
 */
public class ParallelPlayActivity extends AppCompatActivity implements Handler.Callback {
    private NetSpeedTimer mNetSpeedTimer;
    private VideoView player1, player2, player3, player4;
    String VOD_URL1 = "rtmp://192.168.0.141:20002/live/云A65432.蓝色.1.0.1";
    String VOD_URL2 = "rtmp://202.69.69.180:443/webcast/bshdlive-pc";
    String VOD_URL3 = "rtmp://202.69.69.180:443/webcast/bshdlive-pc";
    String VOD_URL4 = "rtmp://202.69.69.180:443/webcast/bshdlive-pc";
    EditText etPlayInput1, etPlayInput2, etPlayInput3, etPlayInput4;
    TextView tvStreamBps1, tvStreamBps2, tvStreamBps3, tvStreamBps4;
    Button btnPlay;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parallel_play);
        //TODO->进来就播放实时视频地址
        String URL= "rtmp://192.168.0.141:20002/live/" + URLEncoder.encode("云A65432.蓝色.1.0.1");
        getUrlPlay(URL, URL, URL, URL);
        etPlayInput1 = findViewById(R.id.etPlayInput1);
        etPlayInput2 = findViewById(R.id.etPlayInput2);
        etPlayInput3 = findViewById(R.id.etPlayInput3);
        etPlayInput4 = findViewById(R.id.etPlayInput4);
        tvStreamBps1 = findViewById(R.id.tvStreamBps1);
        tvStreamBps2 = findViewById(R.id.tvStreamBps2);
        tvStreamBps3 = findViewById(R.id.tvStreamBps3);
        tvStreamBps4 = findViewById(R.id.tvStreamBps4);
        btnPlay = findViewById(R.id.buttonPlay);
        btnPlay.setOnClickListener(v -> {
            String urlPlay1 = etPlayInput1.getText().toString();
            String urlPlay2 = etPlayInput2.getText().toString();
            String urlPlay3 = etPlayInput3.getText().toString();
            String urlPlay4 = etPlayInput4.getText().toString();
            if (TextUtils.isEmpty(urlPlay1)) {
                VOD_URL1 = "rtmp://192.168.0.141:2019/live/012345678912.1";
            } else {
                VOD_URL1 = urlPlay1;
            }
            if (TextUtils.isEmpty(urlPlay2)) {
                VOD_URL2 = "rtmp://192.168.0.141:2019/live/012345678912.2";
            } else {
                VOD_URL2 = urlPlay2;
            }
            if (TextUtils.isEmpty(urlPlay3)) {
                VOD_URL3 = "rtmp://192.168.0.141:2019/live/012345678912.1";
            } else {
                VOD_URL3 = urlPlay3;
            }
            if (TextUtils.isEmpty(urlPlay4)) {
                VOD_URL4 = "rtmp://192.168.0.141:2019/live/012345678912.2";
            } else {
                VOD_URL4 = urlPlay4;
            }
            VideoViewManager.instance().release();
            getUrlPlay(urlPlay1, urlPlay2, urlPlay3, urlPlay4);
            Log.d("Tag", "VOD_URL1:" + VOD_URL1);
            Log.d("Tag", "VOD_URL2:" + VOD_URL2);
            Log.d("Tag", "VOD_URL3:" + VOD_URL3);
            Log.d("Tag", "VOD_URL4:" + VOD_URL4);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Log.d("Tag", "VOD_URL3:" + VOD_URL3);
                }
            }, 1000);
        });
    }

    private void getUrlPlay(String VOD_URL1, String VOD_URL2, String VOD_URL3, String VOD_URL4) {
        //TODO->play1
        player1 = findViewById(R.id.player_1);
        getNetSpeed(player1,1);
        player1.setUrl(VOD_URL1);
        //播放状态监听
        player1.addOnVideoViewStateChangeListener(getOnVideoViewStateChangeListener(player1,1));
        //这两项必须设置
        player1.setEnableAudioFocus(false);
        player1.setEnableParallelPlay(true);
        StandardVideoController controller1 = new StandardVideoController(this);
        player1.setVideoController(controller1);
        player1.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
        controller1.setLive();
        player1.start();
        player1.setMute(true);
        //TODO->play2
        player2 = findViewById(R.id.player_2);
       // getNetSpeed(player2,2);
        player2.setUrl(VOD_URL2);
        //播放状态监听
       // player2.addOnVideoViewStateChangeListener(getOnVideoViewStateChangeListener(player2,2));
        //这两项必须设置
        player2.setEnableAudioFocus(false);
        player2.setEnableParallelPlay(true);
        StandardVideoController controller2 = new StandardVideoController(this);
        player2.setVideoController(controller2);
        player2.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
        controller2.setLive();
        player2.start();
        player2.setMute(true);
        //TODO->play3
        player3 = findViewById(R.id.player_3);
       // getNetSpeed(player3,3);
        player3.setUrl(VOD_URL3);
        //player3.addOnVideoViewStateChangeListener(getOnVideoViewStateChangeListener(player3,3));
        //这两项必须设置
        player3.setEnableAudioFocus(false);
        player3.setEnableParallelPlay(true);
        StandardVideoController controller3 = new StandardVideoController(this);
        player3.setVideoController(controller3);
        player3.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
        player3.start();
        player3.setMute(true);
        //TODO->play4
        player4 = findViewById(R.id.player_4);
        //getNetSpeed(player4,4);
        player4.setUrl(VOD_URL4);
        //player4.addOnVideoViewStateChangeListener(getOnVideoViewStateChangeListener(player4,4));
        //这两项必须设置
        player4.setEnableAudioFocus(false);
        player4.setEnableParallelPlay(true);
        StandardVideoController controller4 = new StandardVideoController(this);
        player4.setVideoController(controller4);
        player4.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
        player4.start();
        player4.setMute(true);
    }

    private void getNetSpeed(VideoView player,int type) {
        Handler handler = new Handler(ParallelPlayActivity.this);
        //创建NetSpeedTimer实例
        mNetSpeedTimer = new NetSpeedTimer(this, type, player, handler).setDelayTime(1000).setPeriodTime(2000);
        //在想要开始执行的地方调用该段代码
        mNetSpeedTimer.startSpeedTimer();
    }
    private OnVideoViewStateChangeListener getOnVideoViewStateChangeListener(VideoView player,int type){
        return new OnVideoViewStateChangeListener() {
            @Override
            public void onPlayerStateChanged(int playerState) {
                switch (playerState) {
                    case PLAYER_NORMAL://小屏
                        break;
                    case VideoView.PLAYER_FULL_SCREEN://全屏
                        break;
                }
            }

            @Override
            public void onPlayStateChanged(int playState) {
                switch (playState) {
                    case VideoView.STATE_IDLE:
                        break;
                    case VideoView.STATE_PREPARING:
                        break;
                    case VideoView.STATE_PREPARED:
                        break;
                    case VideoView.STATE_PLAYING:
                        break;
                    case VideoView.STATE_PAUSED:
                        break;
                    case VideoView.STATE_BUFFERING:
                        break;
                    case VideoView.STATE_BUFFERED:
                        break;
                    case VideoView.STATE_PLAYBACK_COMPLETED:
                        break;
                    case VideoView.STATE_ERROR:
                        break;
                }
            }
        };
    }

    @SuppressLint("SetTextI18n")
    @Override
    public boolean handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch (msg.what) {
            case NetSpeedTimer.NET_SPEED_TIMER_DEFAULT:
                int type = msg.arg1;
                Long speed = (Long) msg.obj;
                if (type == 1) {
                    tvStreamBps1.setText(speed / 3072 + "Kbps");
                }
                if (type == 2) {
                    tvStreamBps2.setText(speed / 3072 + "Kbps");
                }
                if (type == 3) {
                    tvStreamBps3.setText(speed / 3072 + "Kbps");
                }
                if (type == 4) {
                    tvStreamBps4.setText(speed / 3072 + "Kbps");
                }
                //打印你所需要的网速值，单位默认为kb/s
                //tvStreamBps1.setText(speed / 3072 + "Kbps");
                Log.e("Tag", "数据格式类型  = " + type);
                Log.e("Tag", "current net speed  = " + speed + "kb/s");
                break;

            default:
                break;
        }
        return false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        VideoViewManager.instance().pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        VideoViewManager.instance().resume();
    }

    @Override
    protected void onDestroy() {
        if (null != mNetSpeedTimer) {
            mNetSpeedTimer.stopSpeedTimer();
        }
        super.onDestroy();
        VideoViewManager.instance().release();
    }

    @Override
    public void onBackPressed() {
        if (VideoViewManager.instance().onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
